//ParaSite LinkRoll stuff
function Para_LinkRoll( ) {
    this.known_ids   = new Array();
    this.newest_id   = 0;
    this.shout_count = 0;
    this.html        = '';

    this.mode           = 'LIST'; //can be LIST or EDITOR

    this.console = $('para_linkroll_console');
    this.summary = $('para_linkroll_summary');

    this.rpc_response = Para_LinkRoll_RPC_Response;
    this.load_form    = Para_LinkRoll_LoadForm;
    
    this.sev_load_form    = Para_LinkRoll_SEV_LoadForm;
    this.sev_post_link    = Para_LinkRoll_SEV_PostLink;
    this.sev_preview_link = Para_LinkRoll_SEV_PreviewLink;

    this.uev_show_form    = Para_LinkRoll_UEV_ShowForm;
    this.uev_send_link    = Para_LinkRoll_UEV_SendLink;
    this.uev_preview_link = Para_LinkRoll_UEV_PreviewLink;
}


function Para_LinkRoll_RPC_Response( response ) {
    var postback = response.postback;
    switch (postback) {
    case "linkroll_load_form":
        this.sev_load_form( response ); break;
    case "linkroll_post_link":
        this.sev_post_link( response ); break;
    case "linkroll_preview_link":
        this.sev_preview_link( response ); break;
    default:
        alert( "LinkRoll: Unknown postback:" + postback );
    }
}


function Para_LinkRoll_UEV_ShowForm() {
    this.load_form();
}



function Para_LinkRoll_UEV_PreviewLink() {
    var url_field       = $('para_linkroll_input_url');
    var headline_field  = $('para_linkroll_input_headline');
    var cat_id_field    = $('para_linkroll_input_cat_id');

    var url      = url_field.get('value').trim();
    var headline = headline_field.get('value').trim();
    var cat_id   = cat_id_field.get('value');

    para_debug("cat_id=" + cat_id);

    var request = 
        {
            operation: "linkroll_post_link",
            postback:  "linkroll_preview_link",
            param: {
                url: url,
                headline: headline,
                category_id: cat_id,
                preview_only: true
            }
        };
    parapara.send_rpc( request );

}


function Para_LinkRoll_UEV_SendLink() {
    var url_field       = $('para_linkroll_input_url');
    var headline_field  = $('para_linkroll_input_headline');
    var cat_id_field    = $('para_linkroll_input_cat_id');

    var url      = url_field.get('value').trim();
    var headline = headline_field.get('value').trim();
    var cat_id   = cat_id_field.get('value');

    var request = 
        {
            operation: "linkroll_post_link",
            postback:  "linkroll_post_link",
            param: {
                url: url,
                headline: headline,
                category_id: cat_id
            }
        };
    parapara.send_rpc( request );
}


function Para_LinkRoll_SEV_LoadForm( response ) {
    var status = response.status;
    if(status == "ok") {
        var html         = response.retval.html;
        this.mode = 'EDITOR';
        this.console.set( 'html', html );
    }
    else {
        alert("Para_LinkRoll_SEV_Load_Form failed: " + response.error_id );
    }

}


function Para_LinkRoll_SEV_PostLink( response ) {
    var status = response.status;
    if(status == "ok") {
        var html         = response.retval.html;
        this.mode = 'EDITOR';
        this.console.set( 'html', html );
        parapara.status_ok("Tak for dit link. Det bliver muligvis godkendt af en flink admin");
    }
    else {
        parapara.status_warn( response.description );
    }
}


function Para_LinkRoll_SEV_PreviewLink( response ) {
    var status = response.status;
    if(status == "ok") {
        var html         = response.retval.html;
        this.mode = 'EDITOR';
        var preview       = $('para_linkroll_link_preview');
        var preview_inner = $('para_linkroll_link_preview_inner');

        preview_inner.set( 'html', html );
        preview.set('styles', {visibility: 'visible'});
    }
    else {
        parapara.status_warn( response.description );
    }
    
}


function Para_LinkRoll_LoadForm( ) {
   var request = 
        {
            operation: "get_bit",
            postback:  "linkroll_load_form",
            param: {
                bit: "linkroll_link_form"
            }
        };
    parapara.send_rpc( request );
    this.console.set('html', 'Loading Form');
}



//------ EVENT HANDLERS --------------------------------------------------

function para_linkroll_uev_showform( ) {
    parapara.linkroll.uev_show_form( );
}

function para_linkroll_uev_send_link() {
    parapara.linkroll.uev_send_link();
    return false;
}

function para_linkroll_uev_preview_link() {
    parapara.linkroll.uev_preview_link();
    return false;
}

